import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '@/views/Login/Login.vue'
import Home from '@/views/HomePage/Home.vue'
import Category from '@/views/HomePage/Category.vue'
import Cart from '@/views/HomePage/Cart.vue'
import My from '@/views/HomePage/My.vue'

// import HomePageIndex from '@/views/HomePage/HomePageIndex.vue'
// import SearchIndex from '@/views/Search/SearchIndex.vue'
// import SearchListIndex from '@/views/SearchList/SearchListIndex.vue'
// import GoodsDetailsIndex from '@/views/GoodsDetails/GoodsDetailsIndex.vue'
// import PayIndex from '@/views/Pay/PayIndex.vue'
// import OrderManageIndex from '@/views/OrderManage/OrderManageIndex.vue'
// import Address from '@/views/Pay/Address.vue'
// import AddAddress from '@/views/Pay/AddAddress.vue'
// import Temp from '@/views/Pay/temp.vue'
// import store from '@/store'

import SearchIndex from '@/views/Search/SearchIndex.vue'
import SearchListIndex from '@/views/SearchList/SearchListIndex.vue'
import GoodsDetailsIndex from '@/views/GoodsDetails/GoodsDetailsIndex.vue'
import Temp from '@/views/Pay/temp.vue'
import store from '@/store'
const OrderManageIndex = () => import('@/views/OrderManage/OrderManageIndex.vue')
const Address = () => import('@/views/Pay/Address.vue')
const AddAddress = () => import('@/views/Pay/AddAddress.vue')
const PayIndex = () => import('@/views/Pay/PayIndex.vue')
const HomePageIndex = () => import('@/views/HomePage/HomePageIndex.vue')
// import NotFound from '@/views/NotFound.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  {
    path: '/',
    component: HomePageIndex,
    redirect: '/Home',
    children: [
      { path: '/Home', component: Home },
      { path: '/Category', component: Category },
      { path: '/Cart', component: Cart },
      { path: '/My', component: My }
    ]
  },
  { path: '/searchIndex', component: SearchIndex },
  { path: '/searchList', component: SearchListIndex },
  { path: '/goodsDetails/:id', component: GoodsDetailsIndex },
  { path: '/pay', component: PayIndex },
  { path: '/orderManage', component: OrderManageIndex },
  { path: '/address', component: Address },
  { path: '/addAddress', component: AddAddress },
  { path: '/temp', component: Temp }
  // { path: '*', component: NotFound }
]

const router = new VueRouter({
  routes
})

// 所有需登录权限的页面路由
const permissionPage = ['/pay']
router.beforeEach((to, from, next) => {
  // console.log(to, from)
  // 不是权限页面————放行
  if (!permissionPage.includes(to.path)) {
    next()
    return false
  }
  // 是权限页面————强制跳转到登录页
  // store.state.userInfo.userMessage.token ? next() : next('/login')
  store.getters.isLogin ? next() : next('/login')
})

export default router
